<?php

/**
 * This is the model class for table "{{ptproperty}}".
 *
 * The followings are the available columns in table '{{ptproperty}}':
 * @property string $idptproperty
 * @property string $caseno
 * @property string $idccward
 * @property string $idccsex
 * @property string $idptexsumptor
 * @property string $ownername
 * @property string $owneraddress
 * @property string $propertyno
 * @property string $propertyaddress
 * @property string $constyear
 * @property string $totalplotarea
 * @property string $openplotarea
 * @property string $compplotarea
 * @property integer $transferbreakupflag
 * @property integer $deleteflag
 * @property integer $waterharvesting
 * @property string $propertydetails
 *
 * The followings are the available model relations:
 * @property Ccsex $idccsex0
 * @property Ccward $idccward0
 * @property Ptexsumptor $idptexsumptor0
 */
class Ptproperty extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Ptproperty the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{ptproperty}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('ownername, owneraddress, propertyno, constyear, transferbreakupflag, deleteflag, waterharvesting', 'required'),
            array('transferbreakupflag, deleteflag, waterharvesting', 'numerical', 'integerOnly' => true),
            array('caseno, idccward, idccsex, idptexsumptor, constyear', 'length', 'max' => 10),
            array('ownername, owneraddress, propertyaddress', 'length', 'max' => 100),
            array('propertyno', 'length', 'max' => 45),
            array('totalplotarea, openplotarea, compplotarea', 'length', 'max' => 15),
            array('propertydetails', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idptproperty, caseno, idccward, idccsex, idptexsumptor, ownername, owneraddress, propertyno, propertyaddress, constyear, totalplotarea, openplotarea, compplotarea, transferbreakupflag, deleteflag, waterharvesting, propertydetails', 'safe', 'on' => 'search'),
            array('idccward, idccsex, idptexsumptor', 'validatefkeys'),
        );
    }

    public function validatefkeys($attribute, $params) {
        $aux = $attribute . '0';
        if (!$this->$aux)
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'idccsex0' => array(self::BELONGS_TO, 'Ccsex', 'idccsex'),
            'idccward0' => array(self::BELONGS_TO, 'Ccward', 'idccward'),
            'idptexsumptor0' => array(self::BELONGS_TO, 'Ptexsumptor', 'idptexsumptor'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idptproperty' => Yii::t('application', 'Idptproperty'),
            'caseno' => Yii::t('application', 'Caseno'),
            'idccward' => Yii::t('application', 'Idccward'),
            'idccsex' => Yii::t('application', 'Idccsex'),
            'idptexsumptor' => Yii::t('application', 'Idptexsumptor'),
            'ownername' => Yii::t('application', 'Ownername'),
            'owneraddress' => Yii::t('application', 'Owneraddress'),
            'propertyno' => Yii::t('application', 'Propertyno'),
            'propertyaddress' => Yii::t('application', 'Propertyaddress'),
            'constyear' => Yii::t('application', 'Constyear'),
            'totalplotarea' => Yii::t('application', 'Totalplotarea'),
            'openplotarea' => Yii::t('application', 'Openplotarea'),
            'compplotarea' => Yii::t('application', 'Compplotarea'),
            'transferbreakupflag' => Yii::t('application', 'Transferbreakupflag'),
            'deleteflag' => Yii::t('application', 'Deleteflag'),
            'waterharvesting' => Yii::t('application', 'Waterharvesting'),
            'propertydetails' => Yii::t('application', 'Propertydetails'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('idptproperty', $this->idptproperty, true);
        $criteria->compare('caseno', $this->caseno, true);
        $criteria->compare('idccward', $this->idccward, true);
        $criteria->compare('idccsex', $this->idccsex, true);
        $criteria->compare('idptexsumptor', $this->idptexsumptor, true);
        $criteria->compare('ownername', $this->ownername, true);
        $criteria->compare('owneraddress', $this->owneraddress, true);
        $criteria->compare('propertyno', $this->propertyno, true);
        $criteria->compare('propertyaddress', $this->propertyaddress, true);
        $criteria->compare('constyear', $this->constyear, true);
        $criteria->compare('totalplotarea', $this->totalplotarea, true);
        $criteria->compare('openplotarea', $this->openplotarea, true);
        $criteria->compare('compplotarea', $this->compplotarea, true);
        $criteria->compare('transferbreakupflag', $this->transferbreakupflag);
        $criteria->compare('deleteflag', $this->deleteflag);
        $criteria->compare('waterharvesting', $this->waterharvesting);
        $criteria->compare('propertydetails', $this->propertydetails, true);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    public function porpertydetailsDataProvider() {
        if (!$this->propertydetails) {
            $rawData[0]['id'] = 'Ground Floor';
            $rawData[1]['id'] = 'First Floor';
            $rawData[2]['id'] = 'Second Floor';
            $rawData[3]['id'] = 'Third Floor';
            $rawData[4]['id'] = 'Above Third / Basement';
            $rawData[5]['id'] = 'Area on Rent';
            $rawData[6]['id'] = 'Total';

            for ($i = 0; $i <= 6; $i++) {
                $rawData[$i]['aresidential'] = 0;
                $rawData[$i]['acommercial'] = 0;
                $rawData[$i]['bresidential'] = 0;
                $rawData[$i]['bcommercial'] = 0;
                $rawData[$i]['cresidential'] = 0;
                $rawData[$i]['ccommercial'] = 0;
                $rawData[$i]['dresidential'] = 0;
                $rawData[$i]['dcommercial'] = 0;
            }
        } else {
            $rawData = json_decode($this->propertydetails, true);
        }

        $dataProvider = new CArrayDataProvider($rawData, array(
                    'id' => 'porpertydetailsDataProvider',
                ));
        return $dataProvider;
    }    

}